﻿using System;
using PostSharp.Aspects;
using YuYuKiCms.Dal.Model;
using YuYuKiCms.Framework.Extention;
using YuYuKiCms.Framework.UnitTest;

namespace DALTest.Aspect
{
    [Serializable]
    public class InsertPageAttribute : OnMethodBoundaryAspect
    {
        private readonly string _expireDate;
        private readonly int _id;
        private readonly int? _idParent;
        private readonly string _publishDate;
        private readonly bool _published;
        private readonly int _rank;
        private readonly string _title;
        private readonly string _urlTitle;

        #region Overrides of OnMethodBoundaryAspect

        public InsertPageAttribute(
            int id = 0,
            string expireDate = "18/2/2014",
            int idParent = -1,
            int rank = 1,
            string publishDate = "1/1/2011",
            bool published = true,
            string title = "My Title",
            string urlTitle = "my_title")
        {
            _id = id;
            _expireDate = expireDate;
            _idParent = idParent != -1 ? idParent : (int?)null;
            _rank = rank;
            _publishDate = publishDate;
            _published = published;
            _title = title;
            _urlTitle = urlTitle;
        }

        public override void OnEntry(MethodExecutionArgs args)
        {
            var db = new YuyukiModel(Helper.ConnectionString);
            db.Add(new PageMenu
            {
                Id = _id,
                ExpireDate = _expireDate.ToDateTime(),
                IdParent = _idParent,
                Rank = _rank,
                PublishDate = _publishDate.ToDateTime(),
                Published = _published,
                Title = _title,
                UrlTitle = _urlTitle
            });
            db.SaveChanges();
        }

        #endregion
    }
}